﻿/// <reference path="vswd-ext_2.2.js" />
/**
* @author Winston
*/

Ext.namespace('Ext.ux');

/**
*
* @class BMapWindow
* @extends Ext.Window
*/

Ext.ux.BMapWindow = Ext.extend(Ext.Window, {
    deviceID: "",
    trackerName: "",
    MapObject: null,

    initComponent: function() {

        var defConfig = {
            zoomLevel: 3,
            pitch: 0,
            zoom: 0,
            BmapType: 'map',
            border: false,
            items: [new Ext.ux.BMapPanel({ autoHeight: true, autoWidth: true })]
        }

        Ext.applyIf(this, defConfig);

        Ext.ux.BMapWindow.superclass.initComponent.call(this);

    },
    afterRender: function() {
        var wh = this.getSize();
        Ext.applyIf(this, wh);
        Ext.ux.BMapWindow.superclass.afterRender.call(this);
        if (this.BmapType === 'map') {
            this.bmap = this.items.items[0].getMap();
            this.bmap.Resize(this.width - 10, this.height - 30);
            this.bmap.HideMiniMap();
            this.onResize(this.width, this.height);


        }

        if (typeof this.addControl === 'object' && this.gmapType === 'map') {
            this.bmap.AddControl(this.addControl);
        }

        if (typeof this.setCenter === 'object') {
            if (typeof this.setCenter.geoCodeAddr === 'string') {
                this.geoCodeLookup(this.setCenter.geoCodeAddr);
            } else {
                if (this.bmapType === 'map') {
                    var point = new VELatLong(this.setCenter.Latitude, this.setCenter.Longitude);
                    this.bmap.SetCenterAndZoom(point, this.zoomLevel);
                }
                if (typeof this.setCenter.marker === 'object' && typeof point === 'object') {
                    this.addMarker(point, this.setCenter.marker, this.setCenter.marker.clear);
                }
            }
        }

    },

    onResize: function(w, h) {
        if (typeof this.bmap == 'object') {
            if (!this.tbar) {
                this.bmap.Resize(w - 10, h - 30);
            }
            else {

                if (! -[1, ]) {
                    this.bmap.Resize(w - 10, h - 50);
                }
                else {
                    this.bmap.Resize(w, h);
                }
            }
        }

        Ext.ux.BMapWindow.superclass.onResize.call(this, w, h);

    },
    //    setSize: function(width, height, animate) {
    //        if (typeof this.bmap == 'object') {
    //            this.bmap.Resize(this.width, this.height);
    //        }

    //        Ext.ux.BMapWindow.superclass.setSize.call(this, width, height, animate);

    //    },
    getMap: function() {

        return this.bmap;

    },
    addMarkers: function(markers) {

        if (Ext.isArray(markers)) {
            for (var i = 0; i < markers.length; i++) {
                var mkr_point = new VELatLong(markers[i].Latitude, markers[i].Longitude);
                this.addMarker(mkr_point, markers[i].marker, false, markers[i].setCenter);
            }
        }

    },
    addMarker: function(point, marker, clear, center) {

        if (clear === true) {
            this.bmap.DeleteAllShapes();
        }
        if (center === true) {
            this.bmap.SetCenterAndZoom(point, this.zoomLevel);
        }

        var mark = new VEShapeLayer();
        marker.SetPoints(point);
        mark.AddShape(marker);
        this.bmap.AddShapeLayer(mark);

    },
    geoCodeLookup: function(addr) {

        this.bmap.FindLocations(addr, this.addAddressToMap.createDelegate(this));

    },
    addAddressToMap: function(locations) {
        LatLong = locations[0].LatLong
        point = new VELatLng(LatLong.Longitude, LatLong.Latitude);
        if (typeof this.setCenter.marker === 'object' && typeof point === 'object') {
            this.setCenter.marker.SetPoints(point);
            this.AddShapeLayer(this.setCenter.marker);
        }

    }

});

Ext.reg('bmapwindow', Ext.ux.BMapWindow);